package com.app.controller;


import com.app.model.CusItemExpModel;
import com.app.model.CustomerModel;
import com.app.model.UserModel;
import com.app.service.CusEducationService;
import com.app.service.CusItemExpService;
import com.app.service.CustomerService;
import com.app.service.UserService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;


@Controller
@RestController
@Data
public class CusItemExpController {


    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    UserService userService;
    @Autowired
    CustomerService customerService;

    @Autowired
    CusItemExpService cusItemExpService;
    //新增修改
    @PostMapping("/groupItemExp")
    public Result<Object> groupItemExp(HttpServletRequest request, String itemName, String itemContent, String companyName, String startTime, String endTime, int id,String itemDuty,String achievement,String postName) {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);
        if(user==null)
        {
            return Result.error(405,"用户不存在");

        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());

        CusItemExpModel model = new CusItemExpModel();
        model.setItemName(itemName);
        model.setCompanyName(companyName);
        model.setStartTime(startTime);
        model.setEndTime(endTime);
        model.setItemContent(itemContent);
        model.setItemDuty(itemDuty);
        model.setAchievement(achievement);
        model.setPostName(postName);
        model.setCustomerId(cus.getId());
        try {
            if(id>0)
            {
                model.setId(id);
                model.setUpTime(CommonHelp.getNowTimestamp());
                cusItemExpService.update(model);
            }else {
                model.setCrtTime(CommonHelp.getNowTimestamp());
                cusItemExpService.insert(model);
            }
        }catch (Exception e)
        {
            System.out.println(e);
        }
        return Result.success(null);
    }
    //删除
    @PostMapping("/delItemExp")
    public Result<Object> delWorkExp(int id, HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            if(token==null)
            {
                return Result.error(405,"token不存在");
            }
            TokenUtils tokenUtil = new TokenUtils();
            String mobile = tokenUtil.verify(token);
            UserModel user = userService.getUserByMobile(mobile);
            if(user==null)
            {
                return Result.error(405,"用户不存在");
            }
            if(id>0)
            {
                cusItemExpService.delete(id);
            }else {
                return Result.error(500,"ID不存在");

            }

        }catch (Exception e)
        {
            System.out.println(e);
        }
        return Result.success("操作成功");
    }

    //根据用户ID查询项目经验
    @GetMapping("/getItemExpList")
    public Object getItemExpList(HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        String id = request.getParameter("id");

        CustomerModel cus = null;

        if(id!=null&&!"0".equals(id)&&!"undefined".equals(id))
        {
            cus = customerService.getCustomerById(Integer.valueOf(id));
        }else {
            if(token==null)
            {
                return Result.error(405,"token不存在");
            }
            TokenUtils tokenUtil = new TokenUtils();
            String mobile = tokenUtil.verify(token);
            UserModel user = userService.getUserByMobile(mobile);

            if(user==null)
            {
                return Result.error(405,"用户不存在");


            }
            cus = customerService.getCustomerById(user.getCId());
        }
        List<CusItemExpModel> itemExpList = cusItemExpService.getItemExpByCustomerId(cus.getId());

        return Result.success(itemExpList);
    }

    //根据ID查询项目经验
    @GetMapping("/getItemExp")
    public Object getItemExp(HttpServletRequest request) throws ParseException {
        return Result.success(cusItemExpService.getItemExp(request));

    }
}
